import re
import os
import time

import requests

# https://img.hdbz.net/thumbs/2024/1110/150587.jpg?x-oss-process=style/w_auto-h_350
# https://img.hdbz.net/thumbs/2025/0307/158212.jpg?x-oss-process=style/w_auto-h_350
# https://img.hdbz.net/thumbs/2025/0307/158199.png?x-oss-process=style/w_auto-h_350
root = "./imgs"
if not os.path.exists(root):
    os.mkdir(root)
thumb_root = os.path.join(root, "thumb_imgs")
if not os.path.exists(thumb_root):
    os.mkdir(thumb_root)
big_root = os.path.join(root, "big_images")
if not os.path.exists(big_root):
    os.mkdir(big_root)

url = "https://www.hdbz.net/"
res = requests.get(url)
r1 = res.content.decode()
print(r1)
r = re.findall(r'<img src="/templets/images/gray.gif" data-original="(.*?)"', r1)
print(r)

count = 1
# 缩略图
if res.status_code == 200:
    for item in r:
        print(f"正在爬取第{count}张图片")

        thumb_res = requests.get(item)

        with open(f"{thumb_root}/thumb_{count}.jpg", "wb") as f:
            f.write(thumb_res.content)
            count += 1

            time.sleep(1)
else:
    print(f"获取页面数据异常")

# 高清图
count1 = 1
if res.status_code == 200:
    for item in r:
        print(f"正在爬取第{count}张图片")
        item = item.split('?')[0]
        thumb_res = requests.get(item)

        with open(f"{big_root}/big_{count}.jpg", "wb") as f:
            f.write(thumb_res.content)
            count1 += 1

            time.sleep(1)
else:
    print(f"获取页面数据异常")